package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketStorage;
import com.market.bean.MarketStorageExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminStorageCreateVo;
import com.market.mapper.MarketStorageMapper;
import com.market.service.admin.AdminStorageService;
import com.market.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class AdminStorageServiceImpl implements AdminStorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public CommonData<MarketStorage> list(BasePageInfo basePageInfo, String key, String name) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        // 如果不为空，则拼接条件
        if (!StringUtils.isEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }
        // 如果不为空，则拼接条件
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        // 根据sort做order排序
        marketStorageExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        List<MarketStorage> marketStorageList = marketStorageMapper.selectByExample(marketStorageExample);
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorageList);
        return CommonData.data(pageInfo);
    }

    @Override
    public AdminStorageCreateVo create(MultipartFile file) {
        int index = file.getOriginalFilename().indexOf(".");
        String oldName = file.getOriginalFilename().substring(0, index);
        String newLocalFileName = file.getOriginalFilename().replace(oldName, RandomUtil.newLocalFileName());

        File localFile = new File("D:\\project2\\image\\" + newLocalFileName);
        if (!localFile.getParentFile().exists()) {
            localFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(localFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(localFile.getName());
        marketStorage.setName(file.getOriginalFilename());
        marketStorage.setSize((int) file.getSize());
        marketStorage.setType(file.getContentType());
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setUrl("http://localhost:8083/image/" + localFile.getName());
        marketStorage.setDeleted(false);
        marketStorageMapper.insert(marketStorage);

        MarketStorage selectByPrimaryKey = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        AdminStorageCreateVo adminStorageCreateVo = new AdminStorageCreateVo();
        adminStorageCreateVo.setId(selectByPrimaryKey.getId());
        adminStorageCreateVo.setKey(selectByPrimaryKey.getKey());
        adminStorageCreateVo.setName(selectByPrimaryKey.getName());
        adminStorageCreateVo.setSize(selectByPrimaryKey.getSize());
        adminStorageCreateVo.setType(selectByPrimaryKey.getType());
        adminStorageCreateVo.setAddTime(selectByPrimaryKey.getAddTime());
        adminStorageCreateVo.setUpdateTime(selectByPrimaryKey.getUpdateTime());
        adminStorageCreateVo.setUrl(selectByPrimaryKey.getUrl());
        return adminStorageCreateVo;
    }

    @Override
    public MarketStorage update(MarketStorage storage) {
        MarketStorage marketStorage = marketStorageMapper.selectByPrimaryKey(storage.getId());
        marketStorage.setName(storage.getName());
        marketStorage.setUpdateTime(new Date());
        marketStorageMapper.updateByPrimaryKey(marketStorage);
        return marketStorage;
    }

    @Override
    public MarketStorage delete(MarketStorage storage) {
        storage.setDeleted(true);
        storage.setUpdateTime(new Date());
        marketStorageMapper.updateByPrimaryKey(storage);
        MarketStorage marketStorage = marketStorageMapper.selectByPrimaryKey(storage.getId());
        return marketStorage;
    }
}
